/*
 * @(#) GetContentFilter
 *
 * <br> Copyright:  Copyright (c) 2017
 * <br> @author caijinhuang
 * <br> 2017-12-18 16:02:34
 */

package com.sunsharing.sharemeeting.web.filter;

import com.alibaba.fastjson.JSON;
import com.sunsharing.sharemeeting.entity.ResponseModel;
import com.sunsharing.sharemeeting.entity.User;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetContentFilter implements Filter {
    Logger logger = Logger.getLogger(GetContentFilter.class);
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    /*用户登陆验证，并且将请求保存起来然后供aop使用*/
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
        throws IOException, ServletException {
        /*检查登陆状态*/
        String url = ((HttpServletRequest) servletRequest).getServletPath();
        if (url != null && url.indexOf("/login/checkLogin.do") != -1) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            User user = (User) ((HttpServletRequest) servletRequest).getSession().getAttribute("user");
            if (user != null) {
                /**
                 * 判断用户身份，进行请求转发
                 * 如果是普通用户则只可以查询自己相关的会议
                 * 如果是非普通用户则可以查询所有会议内容
                 */
                if (((HttpServletRequest) servletRequest).getServletPath().contains("/ulynlist")) {
                    String title = ((HttpServletRequest) servletRequest).getParameter("title");
                    String skip = ((HttpServletRequest) servletRequest).getParameter("skip");
                    String redirectUrl = ((HttpServletRequest) servletRequest).getRequestURI() + "?" + ((HttpServletRequest) servletRequest).getQueryString().replace("userid","skip");
                    if (title != null && title != "" && skip == null && !user.getRoleName().equals("user")) {
                        ((HttpServletResponse) servletResponse).sendRedirect(redirectUrl);
                    } else if(user.getRoleName().equals("super_admin") && skip == null) {
                        ((HttpServletResponse) servletResponse).sendRedirect(redirectUrl);
                    }
                }
                filterChain.doFilter(servletRequest, servletResponse);
            } else {
                servletResponse.setContentType("text/html;charset=UTF-8");
                Writer writer = servletResponse.getWriter();
                ResponseModel responseModel = new ResponseModel(false, "未登录,请重新登陆", null);
                writer.write(JSON.toJSONString(responseModel));
            }
        }
    }

    public void destroy() {

    }
}
